package Chapter18.tankgame4;


import javax.lang.model.element.VariableElement;
import java.util.Vector;

/**
 * @author 杨森
 * @date 2022/10/31 21:18
 * 敌人的坦克
 */

public class EnemyTank extends Tank implements Runnable {
    Vector<Shot> shots = new Vector<>();
    boolean isLive = true;

    public EnemyTank(int x, int y) {
        super(x, y);
    }

    @Override
    public void run() {
        while (true) {
//            判断如果shots seze()=0，创建一颗子弹，放入到shots集合，并启动
            if (isLive && shots.size() <1) {
                Shot s = null;
//            判断坦克的方向，创建对象的子弹
                switch (getDirect()) {
                    case 0:
                        s = new Shot(getX() + 20, getY(), 0);
                        break;
                    case 1:
                        s = new Shot(getX() + 60, getY()+20, 1);
                        break;
                    case 2:
                        s = new Shot(getX() + 20, getY()+60, 2);
                        break;
                    case 3:
                        s = new Shot(getX(), getY()+20, 3);
                        break;
                    default:
                }
                shots.add(s);
                new Thread(s).start();
            }

//            根据坦克的方向来继续移动
            switch (getDirect()) {
                case 0:
                    for (int i = 0; i < 30; i++) {
                        if (getY() > 0) {
                            moveUp();
                        }

//        休眠50毫秒
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < 30; i++) {
                        if (getX() + 60 < 1000) {

                            moveRight();
                        }
                        //        休眠50毫秒
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                        if (getY() + 60 < 750) {

                            moveDown();
                        }

//        休眠50毫秒
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        if (getX() > 0) {
                            moveLeft();
                        }
//        休眠50毫秒
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                default:
            }

//            随机的改变坦克移动方向0-3
            setDirect((int) (Math.random() * 4));

//            线程退出
            if (!isLive) {
                break;
            }
        }
    }
}
